package com.yaozheng.vocationaltraining.service.impl.userinfo;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.userinfo.IBasicDataView;
import com.yaozheng.vocationaltraining.service.userinfo.BasicDataService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

/**
 * 基础数据获取
 */
@EBean
public class BasicDataServiceImpl implements BasicDataService {
    IBasicDataView iBasicDataView;

    @Override
    public void init(IBasicDataView iBasicDataView) {
        this.iBasicDataView = iBasicDataView;
    }

    @Background
    @Override
    public void getBasicData() {
        try {
            OkHttpClientManager.getInstance().get(
                    Constants.ROOTURL + "/system/base",
                    iBasicDataView.getToken(),
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iBasicDataView) {
                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iBasicDataView.basicDataSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iBasicDataView.basicDataError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iBasicDataView.isResponseResult()) {
                iBasicDataView
                        .basicDataError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }

        }
    }
}
